package com.tokenplus.thai.anetwork

import androidx.lifecycle.ViewModel
import com.tokenplus.thai.adata.coupons.ReqCouponUse
import com.tokenplus.thai.adata.coupons.ReqCouponVerify
import com.tokenplus.thai.data.*
import com.tokenplus.thai.adata.feedback.ReqFeedbackSubmit
import com.tokenplus.thai.adata.home.getInviteFriendEarn.ReqGetInviteFriendEarn
import com.tokenplus.thai.adata.income.ReqExtractInviteIncome
import com.tokenplus.thai.adata.income.ReqGetInviteIncomeData
import com.tokenplus.thai.adata.login.ReqChangePassword
import com.tokenplus.thai.adata.login.ReqLogin
import com.tokenplus.thai.adata.login.ReqRegister
import com.tokenplus.thai.adata.user.ReqGetMyTransactions
import com.tokenplus.thai.adata.user.ReqGetUserWallet
import com.tokenplus.thai.adata.user.ReqSetWalletName
import com.tokenplus.thai.adata.withdraw.ReqWithdrawCreate
import com.tokenplus.thai.utils.IpUtil
import com.tokenplus.thai.utils.UserStateUtil
import kotlinx.coroutines.Dispatchers

object ApiUserReposity : ApiBaseReposity() {

    fun getIpInfo(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response =
                ApiUserNetwork.getIpInfo()
            println(response)
            // 保存
            IpUtil.ipInfo = response
            Result.success(response)
        }

    fun getInviteFriendEarn(
        caller: ViewModel?,
        reqGetInviteFriendEarn: ReqGetInviteFriendEarn
    ) =
        fire(Dispatchers.IO, caller, reqGetInviteFriendEarn.showLoading) {
            val response =
                ApiUserNetwork.getInviteFriendEarn(reqGetInviteFriendEarn)
            if (response.code.equals("0000", true) or response.code.equals("0", true)) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun getInviteIncomeList(
        caller: ViewModel?,
        reqGetInviteFriendEarn: ReqGetInviteFriendEarn
    ) =
        fire(Dispatchers.IO, caller, reqGetInviteFriendEarn.showLoading) {
            val response =
                ApiUserNetwork.getInviteIncomeList(reqGetInviteFriendEarn)
            if (response.code.equals("0000", true) or response.code.equals("0", true)) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun getTransferInDetail(
        caller: ViewModel?,
        reqGetInviteFriendEarn: ReqGetInviteFriendEarn
    ) =
        fire(Dispatchers.IO, caller, reqGetInviteFriendEarn.showLoading) {
            val response =
                ApiUserNetwork.getTransferInDetail(reqGetInviteFriendEarn)
            if (response.code.equals("0000", true) or response.code.equals("0", true)) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun getTransferInEarn(
        caller: ViewModel?,
        reqGetInviteFriendEarn: ReqGetInviteFriendEarn
    ) =
        fire(Dispatchers.IO, caller, reqGetInviteFriendEarn.showLoading) {
            val response =
                ApiUserNetwork.getTransferInEarn(reqGetInviteFriendEarn)
            if (response.code.equals("0000", true) or response.code.equals("0", true)) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun getTransferInEarnList(
        caller: ViewModel?,
        reqGetInviteFriendEarn: ReqGetInviteFriendEarn
    ) =
        fire(Dispatchers.IO, caller, reqGetInviteFriendEarn.showLoading) {
            val response =
                ApiUserNetwork.getTransferInEarnList(reqGetInviteFriendEarn)
            if (response.code.equals("0000", true) or response.code.equals("0", true)) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun getTransferInExtractList(
        caller: ViewModel?,
        reqGetInviteFriendEarn: ReqGetInviteFriendEarn
    ) =
        fire(Dispatchers.IO, caller, reqGetInviteFriendEarn.showLoading) {
            val response =
                ApiUserNetwork.getTransferInExtractList(reqGetInviteFriendEarn)
            if (response.code.equals("0000", true) or response.code.equals("0", true)) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun appVersion(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response =
                ApiUserNetwork.appVersion(reqBase)
            if (response.code.equals("0000", true) or response.code.equals("0", true)) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun getCoins(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response =
                ApiUserNetwork.getCoins(reqBase)
            if (response.code.equals("0000", true) or response.code.equals("0", true)) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun feedbackSubmit(
        caller: ViewModel?,
        reqFeedbackSubmit: ReqFeedbackSubmit
    ) =
        fire(Dispatchers.IO, caller, reqFeedbackSubmit.showLoading) {
            val response =
                ApiUserNetwork.feedbackSubmit(reqFeedbackSubmit)
            if (response.code.equals("0000", true) or response.code.equals("0", true)) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun extractInviteIncome(
        caller: ViewModel?,
        reqExtractInviteIncome: ReqExtractInviteIncome
    ) =
        fire(Dispatchers.IO, caller, reqExtractInviteIncome.showLoading) {
            val response =
                ApiUserNetwork.extractInviteIncome(reqExtractInviteIncome)
            if (response.code.equals("0000", true) or response.code.equals("0", true)) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun extractRechargeIncome(
        caller: ViewModel?,
        reqExtractInviteIncome: ReqExtractInviteIncome
    ) =
        fire(Dispatchers.IO, caller, reqExtractInviteIncome.showLoading) {
            val response =
                ApiUserNetwork.extractRechargeIncome(reqExtractInviteIncome)
            if (response.code.equals("0000", true) or response.code.equals("0", true)) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun getInviteIncomeData(
        caller: ViewModel?,
        reqGetInviteIncomeData: ReqGetInviteIncomeData
    ) =
        fire(Dispatchers.IO, caller, reqGetInviteIncomeData.showLoading) {
            val response =
                ApiUserNetwork.getInviteIncomeData(reqGetInviteIncomeData)
            if (response.code.equals("0000", true) or response.code.equals("0", true)) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun getRechargeIncomeData(
        caller: ViewModel?,
        reqGetInviteIncomeData: ReqGetInviteIncomeData
    ) =
        fire(Dispatchers.IO, caller, reqGetInviteIncomeData.showLoading) {
            val response =
                ApiUserNetwork.getRechargeIncomeData(reqGetInviteIncomeData)
            if (response.code.equals("0000", true) or response.code.equals("0", true)) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun detectUserInfo(
        caller: ViewModel?,
        reqLogin: ReqLogin
    ) =
        fire(Dispatchers.IO, caller, reqLogin.showLoading) {
            val response =
                ApiUserNetwork.detectUserInfo(reqLogin)
            if (response.code.toInt() == 0) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun login(
        caller: ViewModel?,
        reqLogin: ReqLogin
    ) =
        fire(Dispatchers.IO, caller, reqLogin.showLoading) {
            val response =
                ApiUserNetwork.login(reqLogin)
            UserStateUtil.saveMobile(reqLogin.email)
            if (response.code.toInt() == 0) {
                //登录成功后，再重置为非首次安装
                UserStateUtil.saveReInstall(false)
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun register(
        caller: ViewModel?,
        reqRegister: ReqRegister
    ) =
        fire(Dispatchers.IO, caller, reqRegister.showLoading) {
            val response =
                ApiUserNetwork.register(reqRegister)
            if (response.code.equals("0000", true) or response.code.equals("0", true)) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun registerConfirm(
        caller: ViewModel?,
        reqRegister: ReqRegister
    ) =
        fire(Dispatchers.IO, caller, reqRegister.showLoading) {
            val response =
                ApiUserNetwork.registerConfirm(reqRegister)
            if (response.code.equals("0000", true) or response.code.equals("0", true)) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun changePassword(
        caller: ViewModel?,
        reqChangePassword: ReqChangePassword
    ) =
        fire(Dispatchers.IO, caller, reqChangePassword.showLoading) {
            val response =
                ApiUserNetwork.changePassword(reqChangePassword)
            if (response.code.toInt() == 0) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun getUserWallet(
        caller: ViewModel?,
        reqGetUserWallet: ReqGetUserWallet
    ) =
        fire(Dispatchers.IO, caller, reqGetUserWallet.showLoading) {
            val response =
                ApiUserNetwork.getUserWallet(reqGetUserWallet)
            if (response.code.equals("0000", true) or response.code.equals("0", true)) {
                val obj = response.data
                obj.coinType = reqGetUserWallet.coinType
                obj.mainCoinType = reqGetUserWallet.mainCoinType
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun setWalletName(
        caller: ViewModel?,
        reqSetWalletName: ReqSetWalletName
    ) =
        fire(Dispatchers.IO, caller, reqSetWalletName.showLoading) {
            val response =
                ApiUserNetwork.setWalletName(reqSetWalletName)
            if (response.code.equals("0000", true) or response.code.equals("0", true)) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun getUserAccounts(
        caller: ViewModel?,
        reqGetUserWallet: ReqGetUserWallet
    ) =
        fire(Dispatchers.IO, caller, reqGetUserWallet.showLoading) {
            val response =
                ApiUserNetwork.getUserAccounts(reqGetUserWallet)
            if (response.code.equals("0000", true) or response.code.equals("0", true)) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun getMyInviteInfo(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response =
                ApiUserNetwork.getMyInviteInfo(reqBase)
            if (response.code.equals("0000", true) or response.code.equals("0", true)) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun getMyTransactions(
        caller: ViewModel?,
        reqGetMyTransactions: ReqGetMyTransactions
    ) =
        fire(Dispatchers.IO, caller, reqGetMyTransactions.showLoading) {
            val response =
                ApiUserNetwork.getMyTransactions(reqGetMyTransactions)
            if (response.code.equals("0000", true) or response.code.equals("0", true)) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun withdrawCreate(
        caller: ViewModel?,
        reqWithdrawCreate: ReqWithdrawCreate
    ) =
        fire(Dispatchers.IO, caller, reqWithdrawCreate.showLoading) {
            val response =
                ApiUserNetwork.withdrawCreate(reqWithdrawCreate)
            if (response.code.equals("0000", true) or response.code.equals("0", true)) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun withdrawGetBalance(
        caller: ViewModel?,
        reqWithdrawCreate: ReqWithdrawCreate
    ) =
        fire(Dispatchers.IO, caller, reqWithdrawCreate.showLoading) {
            val response =
                ApiUserNetwork.withdrawGetBalance(reqWithdrawCreate)
            if (response.code.equals("0000", true) or response.code.equals("0", true)) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun getRechargeIncomeConfig(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response =
                ApiUserNetwork.getRechargeIncomeConfig(reqBase)
            if (response.code.equals("0000", true) or response.code.equals("0", true)) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun getCoupons(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response =
                ApiUserNetwork.getCoupons(reqBase)
            if (response.code.equals("0000", true) or response.code.equals("0", true)) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun couponUse(
        caller: ViewModel?,
        reqCouponUse: ReqCouponUse
    ) =
        fire(Dispatchers.IO, caller, reqCouponUse.showLoading) {
            val response =
                ApiUserNetwork.couponUse(reqCouponUse)
            if (response.code.equals("0000", true) or response.code.equals("0", true)) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun couponVerify(
        caller: ViewModel?,
        reqCouponVerify: ReqCouponVerify
    ) =
        fire(Dispatchers.IO, caller, reqCouponVerify.showLoading) {
            val response =
                ApiUserNetwork.couponVerify(reqCouponVerify)
            if (response.code.equals("0000", true) or response.code.equals("0", true)) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun getNewComerEnjoy(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response =
                ApiUserNetwork.getNewComerEnjoy(reqBase)
            if (response.code.equals("0000", true) or response.code.equals("0", true)) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun getCheckinDetail(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response =
                ApiUserNetwork.getCheckinDetail(reqBase)
            if (response.code.equals("0000", true) or response.code.equals("0", true)) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun checkin(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response =
                ApiUserNetwork.checkin(reqBase)
            if (response.code.equals("0000", true) or response.code.equals("0", true)) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun getRedMarkInfo(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response =
                ApiUserNetwork.getRedMarkInfo(reqBase)
            if (response.code.equals("0000", true) or response.code.equals("0", true)) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun getFirstRechargeConfig(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response =
                ApiUserNetwork.getFirstRechargeConfig(reqBase)
            if (response.code.equals("0000", true) or response.code.equals("0", true)) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }
}